package com.ruoyi.client.cloudcc.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.kevinsawicki.http.HttpRequest;

import java.util.HashMap;
import java.util.Map;

/**
 * CloudCC接口工具
 */
public class CloudCCAPIUtils {
    private String serviceName = "";
    public static String apiUrl = "";

    private Map<String,Object> paramMap = new HashMap<>();


    //系统参数service

    private CloudCCAPIUtils(String apiUrl,String serviceName){
        this.serviceName = serviceName;
        this.apiUrl = apiUrl;
    }


    public static CloudCCAPIUtils createRequest(String apiUrl,String serviceName){
        return new CloudCCAPIUtils(apiUrl,serviceName);
    }

    /**
     * 添加请求参数
     * @param key       参数名称
     * @param value     参数值
     * @return
     */
    public CloudCCAPIUtils appendParam(String key,Object value){
        paramMap.put(key,value);
        return this;
    }

    /**
     * 设置请求参数
     * @param paramMap  参数map
     * @return
     */
    public CloudCCAPIUtils form(Map<String,Object> paramMap){
        paramMap = paramMap;
        return this;
    }

    /**
     * 获取请求结果
     * @return
     */
    public JSONObject send(){
        String result = HttpRequest.post(apiUrl + "/distributor.action")
                .form(paramMap)
                .form("serviceName",serviceName)
                .body();
        return JSON.parseObject(result);
    }

    public static void main(String[] args) {
        JSONObject result = CloudCCAPIUtils.createRequest("https://k8mm3cmt3235c7ed72cede6e.cloudcc.com/","clogin")
                .appendParam("userName","guanyu@reizinfo.com")
                .appendParam("password","Reiz@2020")
                .send();

        System.out.println(result);
    }
}
